﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Sunboxsoft.Bases.Web
{
    public static class CookieUtility
    {
        private static string RoleNameKey = "bases-role";
        private static string RoleIdKey = "bases-roleid";
        public static void SetRole(string roleId, string roleName)
        {
            HttpCookie cookieId = new HttpCookie(RoleIdKey, roleId);
            cookieId.Expires = DateTime.Now.AddDays(2);
            HttpContext.Current.Response.Cookies.Add(cookieId);

            HttpCookie cookieName = new HttpCookie(RoleNameKey, HttpUtility.UrlEncode(roleName));
            cookieName.Expires = DateTime.Now.AddDays(2);
            HttpContext.Current.Response.Cookies.Add(cookieName);
        }

        public static string GetRoleName()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(RoleNameKey);
            if (cookie == null)
            {
                return null;
            }
            return HttpUtility.UrlDecode(cookie.Value);
        }

        public static string GetRoleId()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(RoleIdKey);
            if (cookie == null)
            {
                return null;
            }
            return cookie.Value;
        }

        public static void RemoveRole()
        {
            HttpCookie cookieId = new HttpCookie(RoleIdKey, string.Empty);
            cookieId.Expires = DateTime.Now.AddDays(-2);
            HttpContext.Current.Response.Cookies.Add(cookieId);

            HttpCookie cookieName = new HttpCookie(RoleNameKey, string.Empty);
            cookieName.Expires = DateTime.Now.AddDays(-2);
            HttpContext.Current.Response.Cookies.Add(cookieName);
        }
    }
}
